package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.FoodVendor;
import com.tourism.mapper.FoodVendorMapper;

import com.tourism.service.FoodVendorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FoodVendorServiceImpl extends ServiceImpl<FoodVendorMapper, FoodVendor> implements FoodVendorService {

    @Override
    public Result getVendorById(Integer id) {
        if (id == null) {
            return Result.fail("ID不能为空");
        }
        FoodVendor vendor = getById(id);
        return Result.success(vendor);
    }

    @Override
    public Result getVendorList(Integer page, Integer pageSize, String name, String location) {
        // 创建分页对象
        Page<FoodVendor> pageParam = new Page<>(page, pageSize);
        // 创建 LambdaQueryWrapper 用于构建查询条件
        LambdaQueryWrapper<FoodVendor> wrapper = new LambdaQueryWrapper<>();

        // 添加模糊查询条件
        if (name != null && !name.isEmpty()) {
            wrapper.like(FoodVendor::getName, name);
        }
        if (location != null && !location.isEmpty()) {
            wrapper.like(FoodVendor::getLocation, location);
        }

        // 执行分页查询
        Page<FoodVendor> pageResult = page(pageParam, wrapper);
        return Result.success(pageResult);
    }

    @Override
    public Result addVendor(FoodVendor foodVendor) {
        // 校验商家名称
        if (foodVendor.getName() == null || foodVendor.getName().isEmpty()) {
            return Result.fail("商家名称不能为空");
        }

        // 保存商家信息
        boolean success = save(foodVendor);
        return success ? Result.success("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result updateVendor(FoodVendor foodVendor) {
        // 校验商家ID
        if (foodVendor.getId() == null) {
            return Result.fail("ID不能为空");
        }

        // 更新商家信息
        boolean success = updateById(foodVendor);
        return success ? Result.success("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deleteVendor(Integer id) {
        // 校验商家ID
        if (id == null) {
            return Result.fail("ID不能为空");
        }

        // 删除商家信息
        boolean success = removeById(id);
        return success ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @Override
    @Transactional
    public Result batchDeleteVendors(List<Integer> ids) {
        // 校验ID列表
        if (ids == null || ids.isEmpty()) {
            return Result.fail("ID列表不能为空");
        }

        // 批量删除商家信息
        boolean success = removeBatchByIds(ids);
        return success ? Result.success("批量删除成功") : Result.fail("批量删除失败");
    }

    @Override
    public Result getAllVendors() {
        // 获取所有商家信息
        List<FoodVendor> vendors = list();
        return Result.success(vendors);
    }
}
